package com.focus.wecenter.service.impl;

import com.focus.wecenter.dao.UserDao;
import com.focus.wecenter.dao.TopicDao;
import com.focus.wecenter.pojo.Topic;
import com.focus.wecenter.pojo.User;
import com.focus.wecenter.service.TopicService;
import com.focus.wecenter.util.Message;
import com.focus.wecenter.util.PageBean;
import com.focus.wecenter.web.vo.SearchResult;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 话题模块业务实现
 *
 * @author Lv Jing
 * @date 2018年01月07日 22:29
 */
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    private TopicDao topicDao;
    @Autowired
    private UserDao userDao;

    @Override
    public List<SearchResult> searchTopic(String query, int limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Topic.class);
        criteria.add(Restrictions.like("topicName", query, MatchMode.ANYWHERE).ignoreCase());
        PageBean<Topic> pageResult = topicDao.findByPage(criteria, 1, limit);
        List<SearchResult> results = new ArrayList<>();
        for (Topic topic : pageResult.getList()) {
            SearchResult result = new SearchResult();
            result.setId(topic.getId());
            result.setContent(topic.getTopicName());
            result.setType("topics");
            result.put("discuss_count", topic.getDiscussCount());
            results.add(result);
        }
        return results;
    }

    @Override
    public PageBean<Topic> findByAll(int currentPage, int pageSize) {
        currentPage = checkCurrentPage(currentPage);
        DetachedCriteria criteria = DetachedCriteria.forClass(Topic.class);
        criteria.addOrder(Order.desc("discussCount"));
        return topicDao.findByPage(criteria, currentPage, pageSize);
    }

    @Override
    public PageBean<Topic> findByWeek(int currentPage, int pageSize) {
        currentPage = checkCurrentPage(currentPage);
        DetachedCriteria criteria = DetachedCriteria.forClass(Topic.class);
        criteria.add(Restrictions.gt("createTime", getTimeBefore(7)));
        criteria.addOrder(Order.desc("discussCount"));
        return topicDao.findByPage(criteria, currentPage, pageSize);
    }

    @Override
    public PageBean<Topic> findByMonth(int currentPage, int pageSize) {
        currentPage = checkCurrentPage(currentPage);
        DetachedCriteria criteria = DetachedCriteria.forClass(Topic.class);
        criteria.add(Restrictions.gt("createTime", getTimeBefore(30)));
        criteria.addOrder(Order.desc("discussCount"));
        return topicDao.findByPage(criteria, currentPage, pageSize);
    }

    @Override
    public PageBean<Topic> findByRecently(int currentPage, int pageSize) {
        currentPage = checkCurrentPage(currentPage);
        DetachedCriteria criteria = DetachedCriteria.forClass(Topic.class);
        criteria.addOrder(Order.desc("createTime"));
        return topicDao.findByPage(criteria, currentPage, pageSize);
    }

    @Override
    public Topic findTopicByID(int topicID) {
        return topicDao.findById(topicID);
    }

    @Override
    public PageBean<Topic> findByPage(DetachedCriteria criteria, int currentPage, int pageSize) {
        currentPage = checkCurrentPage(currentPage);
        return topicDao.findByPage(criteria, currentPage, pageSize);
    }

    @Override
    public Message focus(int topicID, int userID) {
        Message message = new Message();
        if (userID == -1) {
            message.setSuccess(false);
            message.setData("请先登录");
            return message;
        }
        // 查找用户
        User user = userDao.findById(userID);
        if (user == null) {
            message.setSuccess(false);
            message.setData("请先登录");
            return message;
        }
        // 查找问题
        Topic topic = topicDao.findById(topicID);
        if (topic == null) {
            message.setSuccess(false);
            message.setData("话题不存在");
            return message;
        }
        // 判断用户是否关注该问题
        if (user.getFocusTopics().contains(topic)) {
            // 关注了就取关
            user.getFocusTopics().remove(topic);
            user.setTopicFocusCount(user.getTopicFocusCount() - 1);
            topic.setConcernNumber(topic.getConcernNumber() - 1);
            message.setSuccess(true);
            message.setData("remove");
            return message;
        }
        // 用户与问题关联
        user.getFocusTopics().add(topic);
        user.setTopicFocusCount(user.getTopicFocusCount() + 1);
        topic.setConcernNumber(topic.getConcernNumber() + 1);
        message.setSuccess(true);
        message.setData("add");
        return message;
    }

    @Override
    public boolean isFocus(int topicID, int userID) {
        User user = userDao.findById(userID);
        if (user == null) {
            return false;
        }
        Topic topic = new Topic();
        topic.setId(topicID);
        return user.getFocusTopics().contains(topic);
    }

    private int checkCurrentPage(int currentPage) {
        if (currentPage < 1) {
            currentPage = 1;
        }
        return currentPage;
    }

    private Date getTimeBefore(int day) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, - day);
        Date monday = c.getTime();
        String format = sdf.format(monday);
        try {
            return sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return monday;
    }
}
